package com.deYang.app.ljodfh.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;

import com.deYang.app.ljodfh.beans.FocusColumn;
import com.deYang.app.ljodfh.ui.fragment.AnchorFragment;
import com.deYang.app.ljodfh.ui.fragment.CityStateFragment;
import com.deYang.app.ljodfh.ui.fragment.ColumnBaseFragment;
import com.deYang.app.ljodfh.ui.fragment.CommonFragment;
import com.deYang.app.ljodfh.ui.fragment.GreatestFragment;
import com.deYang.app.ljodfh.ui.fragment.LiveFragment;
import com.deYang.app.ljodfh.ui.fragment.NewsListFragment;
import com.deYang.app.ljodfh.ui.fragment.TvFragment;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by Administrator on 2017/6/19 0019.
 */

public class HomePagerAdapter extends FragmentStatePagerAdapter {

    public static final int TYPE_SIFT = 1;//精选
    public static final int TYPE_NEWS = 2;//新闻、要闻
    public static final int TYPE_BTV = 3;//卫视
    public static final int TYPE_LIVE = 4;//直播
    public static final int TYPE_ANCHOR = 5;//主播
    public static final int TYPE_CITY = 6;//市州
    public static final int TYPE_COMMON = 7;//公共


    private List<FocusColumn> list;

    private SparseArray<WeakReference<ColumnBaseFragment>> fragmentWeaks;

    private int topHeight;

    public HomePagerAdapter(FragmentManager fm, List<FocusColumn> list) {
        super(fm);
        this.list = list;
        fragmentWeaks = new SparseArray<>();
    }

    @Override
    public Fragment getItem(int position) {
        ColumnBaseFragment fragment = null;
        switch (list.get(position).getChannelType()) {
            case "" + TYPE_SIFT:
                fragment = new GreatestFragment();
                break;
            case "" + TYPE_NEWS:
                fragment = new NewsListFragment();
                break;
            case "" + TYPE_BTV:
                fragment = new TvFragment();
                break;
            case "" + TYPE_LIVE:
                fragment = new LiveFragment();
                break;
            case "" + TYPE_ANCHOR:
                fragment = new AnchorFragment().setOtherHeight(topHeight);

                break;
            case "" + TYPE_CITY:
                fragment = new CityStateFragment();
                break;
            case "" + TYPE_COMMON:
            default:
                fragment = new CommonFragment();
                break;
        }
        fragment.setChannelUrl(list.get(position).getChannelUrl());
        fragment.setIndexInViewPager(position);
        fragmentWeaks.put(position, new WeakReference<ColumnBaseFragment>(fragment));
        return fragment;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position).getChannelName();
    }

    public void setData(List<FocusColumn> data) {
        this.list = data;
        notifyDataSetChanged();
    }

    public ColumnBaseFragment getFragmentByPos(int pos) {
        WeakReference<ColumnBaseFragment> wrf = fragmentWeaks.get(pos);
        if (wrf != null) {
            return wrf.get();
        }
        return null;
    }

    public int getPosCurrentPage(String type) {
        if (getCount() == 0) {
            return -1;
        }
        for (int i = 0; i < getCount(); i++) {
            if (list.get(i).getChannelType().equals(type)) {
                return i;
            }
        }
        return -1;
    }

    public void setTopHeight(int topHeight) {
        this.topHeight = topHeight;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }
}
